// JavaScript Document
var city_array = {
    110: '上海',
    641: '杭州',
    665: '苏州',
    719: '西安'
};

var ip_city_name = '上海';
var ip_city_id = 110;
var citys = [];
$(function () {

    ip_city_name = remote_ip_info["city"];

    $.each(city_array,function(id,name){
        if(name == ip_city_name)
        {
            citys.unshift(name);
            ip_city_id = id;
            ip_city_name = name;
        }
        else
        {
            citys.push(name);
        }

    });

    if (!E.getCookie('cookie_city_id') && !E.getCookie("cookie_temp_city_id")) {
        LandingDialog_Fun();
    }else{
        if(E.getCookie('EBSIG_CITY_ID')!=ip_city_id && !E.getCookie("cookie_temp_city_id")){
            LandingDialog_Fun();
        }
    }

});

//第一次进入弹框选择城市
function LandingDialog_Fun() {
    $("body").fadeIn(function(){
        var html = "";

        $.each(citys,function(id,name){
            html += "<option value='"+name+"'>"+name+"</option>";
        });

        var Content="<form action='#' method='POST'><ul class='list_c'>";
        Content=Content+"<li class='Eject_title'><p>获取位置</p></li>";
        Content=Content+"<li class='Ejectbox_hy'><p>您当前所在位置：<label id='labelID'>"+citys[0]+"</label><br>是否选择此位置为配送城市</p></li>";
        Content=Content+"<li class='Ex_btn_box'><a class='to_city'><select id='selectID' onchange='selectChang(this)'>" + html + "</select></a><input type='button' value='确认'  class='input_style ok_default'></li>";
        Content=Content+"</form>";
        if($.inArray(ip_city_name,citys) < 0){

            Content="<form action='#' method='POST'><ul class='list_c'>";
            Content=Content+"<li class='Eject_title'><p>获取位置</p></li>";
            if(ip_city_name==""||ip_city_name==null){
                Content=Content+"<li class='Ejectbox_hy'><p>无法获取您当前所在位置<br>请选择商品配送的城市</p></li>";
            }else{
                Content=Content+"<li class='Ejectbox_hy'><p>您当前所在位置：<label id='labelID'>"+citys[0]+"</label><br>目前仅支持上海、杭州、苏州配送</p></li>";
            }
            Content=Content+"<li class='Ex_btn_box'><a class='to_city'><select id='selectID' onchange='selectChangWap(this)'>" + html + "</select></a><input type='button' value='随便看看'  id='kankan_default'></li>";
            Content=Content+"</form>";
        }

        My_Feedback(Content);
    });
}

//弹出城市选择框
function My_Feedback (msg) {
    try {
        $("div[class='tc-con']").remove();
        $("div[class='tc-cover']").remove();
        $cover = $("<div class='tc-cover'id='myCover'>&nbsp;</div>");
        $main = $("<div class=\"tc-con\" id='maximPanel'></div>");
        $close = $("<a href='javascript:void(0);' class='close close_2'>关闭</a>");
        $content = $("<div  class='tc-con-1'></div>");
        $content.html(msg);
        $main.append($close);
        $main.append($content);
        $(document.body).append($cover);
        $(document.body).append($main);
        $cover.height($(document.body).height());
        $main.show(400);
        $main.focus();
        $(".tc-cover").height($(document).height());
        var tw=$(".tc-con").width(),
            th=$(".tc-con").height(),
            ul_h=$(".red_list").height();


        $(".tc-con").css({"margin-left":-(tw/2),"margin-top":-(th/2)});

        $(".Feedback_box ul li").on("click",function(){
            if($(this).attr("class")=="cur"){
                $(this).removeClass("cur");
            }else{
                $(this).addClass("cur");
            };
        });

        //确定
        $("#b_close,.ok_default").click(function(){

            var cityid=110;
            var cityname=$("#selectID").val();

            $.each(city_array,function(id,name){
                if(name  == cityname)
                {
                    cityid = id;
                }
            });

            E.setCookie("cookie_city_id", cityid, 8640000, G.args.cookie_domain);
            E.setCookie("EBSIG_CITY_ID", cityid, 8640000, G.args.cookie_domain);
            E.setCookie("cookie_temp_city_id", cityid, 0, G.args.cookie_domain);
            $main.hide(300);
            $main.remove();
            $cover.remove();
            document.location.reload();
        });

        //随便看看
        $("#kankan_default").click(function(){
            var cityid=110;
            E.setCookie("EBSIG_CITY_ID", cityid, 8640000, G.args.cookie_domain);
            E.setCookie("cookie_temp_city_id", cityid, 0, G.args.cookie_domain);
            $main.hide(300);
            $main.remove();
            $cover.remove();
            document.location.reload();
        });

        //关闭
        $close.click(function () {
            var cityid=110;
            $.each(city_array,function(id,name){
                if(name  == ip_city_name)
                {
                    cityid = id;
                }
            });


            E.setCookie("EBSIG_CITY_ID", cityid, 8640000, G.args.cookie_domain);
            E.setCookie("cookie_temp_city_id", cityid, 0, G.args.cookie_domain);
            document.location.reload();
            $main.hide(300);
            $main.remove();
            $cover.remove();
        });
    }
    catch (ex) { alert(ex); }

}

//下拉框选择事件
function selectChang(obj){
    $("#labelID").html($(obj).val());
}
function selectChangWap(obj){
    $("#labelID").html($(obj).val());

    var cityid=110;
    var cityname=$("#selectID").val();

    $.each(city_array,function(id,name){
        if(name  == cityname)
        {
            cityid = id;
        }
    });

    E.setCookie("cookie_city_id", cityid, 8640000, G.args.cookie_domain);
    E.setCookie("EBSIG_CITY_ID", cityid, 8640000, G.args.cookie_domain);
    E.setCookie("cookie_temp_city_id", cityid, 0, G.args.cookie_domain);
    $main.hide(300);
    $main.remove();
    $cover.remove();
    document.location.reload();
}





